package com.sigmatrix.h5.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sigmatrix.h5.dao.DisRoleResDao;
import com.sigmatrix.h5.entity.DisRoleRes;
import com.sigmatrix.h5.entity.DisSysRes;
import com.sigmatrix.h5.entity.DisSysResDetail;
import com.sigmatrix.h5.enums.WalletType;
import com.sigmatrix.h5.service.DisRoleResService;
import com.sigmatrix.h5.service.DisSysResDetailService;
import com.sigmatrix.h5.service.DisSysResService;
import com.sigmatrix.h5.utils.Assert;
import com.sigmatrix.sc.jdbc.common.service.ServiceImpl;

/**
 * 
 *@ClassName: DistributionRoleResServiceImpl
 *@Description: CRM角色权限控制
 *@author MH
 *@date 2017年11月13日 下午5:28:58
 */
@Service
public class DisRoleResServiceImpl extends ServiceImpl<DisRoleRes, Integer> implements DisRoleResService{
	
	@Autowired
	private DisSysResService distributionSysResService;
	@Autowired
	private DisRoleResDao disRoleResDao;
	@Autowired
	private DisSysResDetailService disSysResDetailService;
	@Override
	public void grantAllRes(Integer dealerId, Integer acctRoleId) {
		//查询所有的功能权限
		List<DisSysRes> sysRes = distributionSysResService.select(null);
		Assert.notNull(sysRes, "该企业没有创建CRM功能权限");
		DisSysResDetail resDetail = disSysResDetailService.findByCode(WalletType.PERSONAL_WALLET.name());
		Assert.notNull(resDetail, "该企业没有零钱包类型");
		for (DisSysRes res : sysRes) {
			DisRoleRes roleRes = new DisRoleRes();
			roleRes.setAccountRoleId(acctRoleId);
			roleRes.setDealerId(dealerId);
			roleRes.setResourceId(res.getId());
			roleRes.setResDetailIds(resDetail.getId()+"");
			insertSelective(roleRes);
		}
	}

	@Override
	public DisRoleRes checkPremission(Integer userId, String eseCode, String moduleCode) {
		return disRoleResDao.checkPermission(userId, eseCode, moduleCode);
	}
	
}
